<?php

include ('../Controllers/dbPlayer.php');
include ('../Controllers/Utilities.php');

main ( );

/**
 * Main function that calls all other functions and processes post/get data.
 */
function main ( )
{
	$id = 0;
	displayForm ( );
	if(isset ($_POST['subEdit_x']) && isset($_POST['subId']) && isset($_POST['subName'])){
		updatePlayer();
		echo("It worked");
	}
	if(isset($_GET['delete'])) {
		deletePlayer($_GET['delete']);
	}
	if(isset ($_POST['addPlayer'])){
		$id = addPlayer( );
	}
	displayPlayerTableData($priv);
}
function displayForm( )
{
	if(isset ($_POST['addPlayer'])){
		echo <<<_END
  <p class="paragraph"> Added a program named:" {$_POST['firstName']} "</p>
_END;
	}
}

/**
 * Add a player
 */
function addPlayer( )
{
	$firstName = $_POST['firstName'];
	$lastName = $_POST['lastName'];
	$age = $_POST['age'];
	$position = $_POST['position'];
	
	$playerAttributes = array('playerID' => 'null', 'firstName'=> $firstName, 'lastName'=> $lastName, 'age'=> $age, 'position'=> $position);
	$player = new Designation($playerAttributes);
	$id = dbPlayer::addPlayer($player);
	return $id;
}

function deletePlayer($id)
{
	$players = dbPlayer::getPlayers("player", $_GET['delete']);
	if(COUNT($player) == 0) {
		dbPlayer::deletePlayer($id);
		echo("<br />Player Deleted<br /><br />");
	}
	else {
		echo("Cannot delete: There are Games in the system using this Player. Please delete or update all related Games first before deleting this Player.<br />");
	}
}

/**
 * Update a player
 */
function updatePlayer()
{
	$name = $_POST['subName'];
	$id = $_POST['subId'];
	dbPlayer::updatePlayer($name, $id);
	echo("Player Updated");
}

?>